<template>
  <!-- 科技成果台账 -->
  <base-breadcrumb :title="titleName">
    <searchData ref="searchArr" :search-data="searchArr" @search="search" @reset="achievementClassify = ''" />
    <a-card :bordered="false">
      <list-table
        ref="table"
        :columns="columns"
        :request="loadData"
        :pageKeys="['pageNum', 'pageSize', 'totalSize']"
         :select-config="{use: true, type: 'checkbox', rowKey: 'id'}"
        :exportConfig="{ use: true, select: '科技成果台账', all: '科技成果台账' }"
        :is-show-count-bar="false"
        :exportPermission="{ select: 'stm_013_exp_03', all: 'stm_013_expall_04' }"
        @select-click="selectClick"
      >
        <!-- :exportPermission="{ select: 'stm_005_exp_07', all: 'stm_005_expall_08' }" -->
        <template slot="bottom">
          <AuditSummary ref="summary" :form="queryParam" :load-count="loadCount" :api="api" :count-list="countList" :selectLength="selectLength" />
        </template>
        <template slot="index" slot-scope="text, record, index">{{ index + 1 }} </template>
        <div slot="action" slot-scope="text, record">
          <MoreButton>
          <a v-btn-permission="'stm_013_loo_01'" @click="handleAction('view', record.id)">查看</a>
          <!-- 来源为历史数据才能编辑 -->
          <a v-if="record.source == 1" v-btn-permission="'stm_013_edi_02'" @click="handleAction('edit', record.id)">编辑</a>
          </MoreButton>
        </div>
      </list-table>
    </a-card>
  </base-breadcrumb>
</template>

<script>
import { getPageAchievementsLedgerBySearch, summaryCount, queryProjectAndBranch, titleLabelQuery, removeDeclare, exportCompleteData, exportCompleteData2 } from '@/api/achievementsManage'
import SelectAchievementType from '@/components/Select/SelectAchievementType' // 成果分类
import SelectAchievementLevel from '@/components/Select/SelectAchievementLevel' // 成果等级
import AuditSummary from './components/AuditSummaryAccount'
import OrgTreeSelect from '@/components/OrgTreeSelect'

const columns = [
  {
    title: '序号',
    scopedSlots: { customRender: 'index' },
    dataIndex: 'index',
    width: 70
  },
  {
    title: '所属分公司',
    dataIndex: 'branchCompanyName',
    width: 120,
    key: 'branchCompanyName'
  },
  {
    title: '项目名称',
    dataIndex: 'projectName',
    width: 250,
    key: 'projectName'
  },
  {
    title: '成果名称',
    width: 200,
    dataIndex: 'achievementName',
    key: 'achievementName'
  },
  {
    title: '成果等级',
    width: 200,
    dataIndex: 'achievementLevel',
    key: 'achievementLevel'
  },
  {
    title: '成果分类 ',
    width: 140,
    dataIndex: 'achievementClassify',
    key: 'achievementClassify'
  },
  {
    title: '获得时间',
    dataIndex: 'gainTime',
    width: 120,
    key: 'gainTime'
    // customRender(text, row) {
    //   return row.completeStatusCode == 4 ? text : ''
    // }
  },
  {
    title: '完成人',
    width: 200,
    dataIndex: 'completeName',
    key: 'completeName'
    // customRender(text, row) {
    //   if (row.newCompleteEmployee && row.newCompleteEmployee.length) {
    //     return row.newCompleteEmployee.map((v) => v.employeeName).join(',')
    //   }
    //   return ''
    // }
  },
  {
    title: '来源',
    width: 200,
    dataIndex: 'sourceText'
  },
  {
    title: '操作',
    fixed: 'right',
    width: 220,
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'a' + Date.now(),
  components: {
    AuditSummary
  },
  data() {
    this.columns = columns
    return {
      loadCount: 0, // 汇总加载控件
      countList: [
        {
          title: '已选择：',
          value: 'selectLength'
        },
        {
          title: '总数：',
          value: 'total'
        }
      ], //汇总列表
      api: { count: summaryCount },
      titleName: '',
      queryParam: {},
      selectLength:0,
      loadData: (parameter) => {
        this.$refs.summary?.refresh()
        const requestParameters = Object.assign({}, parameter, { param: { ...this.queryParam } })
        // 添加成果分类查询条件
        if (this.achievementClassify) {
          requestParameters.param.achievementClassify = this.achievementClassify
          requestParameters.param.achievementClassifyId && delete requestParameters.param.achievementClassifyId
        }
        return getPageAchievementsLedgerBySearch(requestParameters).then((res) => {
          if (res.code === 200) {
            this.$refs.summary.countInfo = { total: res.totalSize || 0 }
            res.data.forEach((v) => {
              v.sourceText = v.source === 1 ? '历史数据' : '录入'
            })
          }
          return res
        })
      },
      searchArr: [
        // 搜索框, 不用新组件所属单位
        // {
        //   name: '所属单位',
        //   valueKey: 'searchQuery',
        //   type: OrgTreeSelect,
        // },
        {
          name: '所属分公司',
          value: 'branchCompanyName',
          placeholder: '请选择',
          type: 'select',
          data: []
        },
        {
          name: '项目名称',
          placeholder: '请选择',
          value: 'projectName',
          type: 'select',
          data: [],
          attrs: {
            showSearch: true
          }
        },
        {
          name: '成果名称',
          value: 'achievementName',
          type: 'input'
        },
        {
          name: '成果分类',
          valueKey: 'achievementClassifyId', // 这里用的表单是id，因为成果等级是根据成果分类id查询的，但是搜索又要用到成果分类名称，很奇葩，但是没办法，所以这里用id
          placeholder: '请选择',
          type: SelectAchievementType,
          replaceFields: {
            name: 'name',
            key: 'id'
          },
          listeners: {
            change: (e,VNODE) => {
              this.achievementClassify = VNODE?.componentOptions.children[0]?.text
              // 重置成果等级
              this.$refs?.searchArr.form.resetFields(['achievementLevel'])
            }
          }
        },
        {
          name: '成果等级',
          placeholder: '请选择',
          valueKey: 'achievementLevel',
          keyValue: 'achievementClassifyId',
          type: SelectAchievementLevel,
          replaceFields: {
            name: 'levelName',
            key: 'levelName'
          }
        },
        {
          name: '获得时间',
          value: 'gainTime',
          type: 'dateRange',
          attr: { rangeFormat: ['gainStartTime', 'gainEndTime'] }
        },
        {
          name: '完成人',
          value: 'completeName',
          type: 'input'
        }
      ],
      achievementClassify: '' //成果分类
    }
  },
  filters: {
    statusFilter(type) {
      return statusMap[type].text
    },
    statusTypeFilter(type) {
      return statusMap[type].status
    }
  },
  created() {
    this.getSubCompanyAndProject()
  },
  methods: {
    selectClick(a,b){
      this.selectLength = a.rows.length;
    },
    /**
     * @description: 获取分公司和项目
     * @return {*}
     */
    getSubCompanyAndProject() {
      queryProjectAndBranch().then((res) => {
        this.searchArr[0].data = res.data?.branchComList?.map((i) => ({ label: i, value: i }))
        this.searchArr[1].data = res.data?.projectList?.map((i) => ({ label: i, value: i }))
      })
    },
    search(val) {
      if (val) {
        this.queryParam = val
      }
      this.$refs.table.getData(null, true)
      this.loadCount++
    },
    /**
     * @description:
     * @param type：操作类型：view：查看，edit：编辑
     * @param id：操作id
     * @return {*}
     */
    handleAction(type, id) {
      console.log('type, idtype, id',type, id)
      let query = {
        type: type
      }
      if (id) {
        query.id = id
      }
      this.$router.push({
        path: '/science/accountDetails',
        query
      })
    }
  }
}
</script>